<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once(TIN_APP_PATH . 'include/TinAppController.php');
require_once (TIN_APP_PATH . 'include/Auth.php');

class AjaxController extends TinAppController {
    
	private $user;
	
    public function __construct() {
        parent::__construct();
        T::$tplFile = "public/empty";
        $this->user = Auth::getUserInfo();        
        if(!$this->user || $this->user==null){
        	$result = array();
        	$result["code"] = "error";
        	$result["msg"] = "您还没有登录或登录超时";
        	echo $this->getResources($result);
        	exit();
        }
    }
    
    /** 上传用户头像
     * @author ZhangXiLian 2014-09-15*/
    public function actionUpdateHeadimg(){
    	$result = array();
		$result["code"] = "error";
		$result["msg"] = "更新异常";
		
    	$userId = $this->user['ID'];
		$name = RequestUtil::getPost('name');
		$imgid = RequestUtil::getPost('imgid');
		if($name && $imgid){
			$dataArr = array();
			$dataArr['HEADER_URL'] = "'$name'";
						
			$userLogic = new UserLogic();
			$u_res = $userLogic->updateFieldVal($dataArr, "ID=?", array($this->user['ID']));
			if($u_res){
				$result["code"] = "success";
				$result["msg"] = "更新头像成功。";
			}else{
				$result["code"] = "error";
				$result["msg"] = "更新头像异常，请重试！";
			}
		}
		echo $this->getResources($result);
    }
    /** 更新身份证图片
     * */
    public function actionUpdateidcardimg(){
    	$result = array();
    	$result["code"] = "error";
    	$result["msg"] = "更新异常";
    	
    	$userId = $this->user['ID'];
    	$name = RequestUtil::getPost('name');
    	$imgid = RequestUtil::getPost('imgid');
    	if($name && $imgid){
    		$dataArr = array();
    		$dataArr['IDCARD_URL'] = "'$name'";
    	
    		$userLogic = new UserLogic();
    		$u_res = $userLogic->updateFieldVal($dataArr, "ID=?", array($this->user['ID']));
    		if($u_res){
    			$result["code"] = "success";
    			$result["msg"] = "更新身份证图片成功。";
    		}else{
    			$result["code"] = "error";
    			$result["msg"] = "更新身份证图片异常，请重试！";
    		}
    	}
    	echo $this->getResources($result);
    }    
    /** 修改密码
     * @author ZhangXiLian 2014-09-15 */
    public function actionUpdatePasswd(){
    	$result = array();
    	$result["code"] = "error";
    	$result["msg"] = "未知参数";
    	
    	if($_POST['btnsubmit']){
    		$oldpass = RequestUtil::getPost('oldpass');
    		$newpass = RequestUtil::getPost('newpass'); 
    		$newpass1 = RequestUtil::getPost('newpass1');
    		if($oldpass!="" && $newpass!="" && $newpass1!=""){
    			if($newpass==$newpass1){
	    			$userLogic = new UserLogic();
	    			$userItem = $userLogic->getUserItem("USERNAME", "ID=? and PASSWORD=?", 
	    					array($this->user['ID'], md5($oldpass)));
	    			if($userItem){
	    				$dataArr = array();
	    				$dataArr['PASSWORD'] = md5($newpass);
	    				$userLogic->updateFieldVal($dataArr, "ID=?", array($this->user['ID']));
	    				$result["code"] = "success";
	    				$result["msg"] = "更新密码成功";
	    			}else{
	    				$result["code"] = "error";
	    				$result["msg"] = "旧密码错误。";
	    			}
    			}else{
    				$result["code"] = "error";
    				$result["msg"] = "输入的两次新密码不一致。";
    			}
    		}
    	}
    	echo $this->getResources($result);
    }
    
}
?>
